<!--
 * @Author: Zcl84765
 * @Date: 2022-11-16 09:39:18
 * @LastEditors: Zcl84765
 * @LastEditTime: 2023-01-19 10:19:00
-->
<template>
  <div>
    <el-form :model="uform" ref="ruleForm" :rules="userRuleForm">
      <el-form-item prop="uersList">
        <el-table :data="uform.uersList">
          <el-table-column label="序号" width="50">
            <template scope="scope">
              <span>{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" width="180">
            <template scope="scope">
              <el-form-item
                :prop="'uersList.' + scope.$index + '.name'"
                :rules="userRuleForm.name"
              >
                <el-input v-model="scope.row.name"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="爱好" width="180">
            <template scope="scope">
              <el-form-item>
                <el-input v-model="scope.row.hoby"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="毕业院校" width="220">
            <template scope="scope">
              <el-form-item>
                <el-input v-model="scope.row.school"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button
                v-if="scope.$index > 0"
                type="text"
                size="small"
                @click="deletUser(uform.uersList, scope.$index)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-edit"
          @click="addUserInfo(uform.uersList)"
        >
          添加用户信息
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      uform: {
        uersList: [{ name: "", hoby: "", school: "" }]
      },
      userRuleForm: {
        uersList: [
          { required: true, message: "请填写信息", trigger: "change" }
        ],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
            console.log(this.uform);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addUserInfo(userList) {
      userList.push(
        Object.assign(
          {},
          {
            name: "",
            hoby: "",
            school: ""
          }
        )
      );
    },
    deletUser(userList, idx) {
      userList.splice(idx, 1);
    }
  }
};
</script>

<style lang="less" scoped>

</style>
